﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SSS.Core.Model
{
    /// <summary>
    /// 管理员信息
    /// </summary>
    [Serializable]
    public class User
    {
        #region 字段

        private string id;
        private string pwd;
        private string email;
        private string roleName;
        private string lastLoginIp;
        private string loginIp;
        private DateTime lastLoginTime;
        private DateTime loginTime;
        private string extension;
        private string format = "[{0}\"UserId\":{1},\"UserPassword\":{2},\"UserEmail\":{3},\"UserRole\":{4},\"LastLoginIp\":{5},\"LastLoginTime\":{6},\"LoginIp\":{7},\"LoginTime\":{8}{9}]";

        #endregion

        #region 属性

        /// <summary>
        /// 管理员id
        /// </summary>
        public string Id
        {
            get { return id; }
            set { id = value; }
        }

        /// <summary>
        /// 管理员密码
        /// </summary>
        public string Password
        {
            get { return pwd; }
            set { pwd = value; }
        }

        /// <summary>
        /// 管理员邮箱
        /// </summary>
        public string Email
        {
            get { return email; }
            set { email = value; }
        }

        /// <summary>
        /// 管理员角色
        /// </summary>
        public string RoleName
        {
            get { return roleName; }
            set { roleName = value; }
        }

        /// <summary>
        /// 上次登录ip
        /// </summary>
        public string LastLoginIp
        {
            get { return lastLoginIp; }
            set { lastLoginIp = value; }
        }

        /// <summary>
        /// 登录ip
        /// </summary>
        public string LoginIp
        {
            get { return loginIp; }
            set { loginIp = value; }
        }

        /// <summary>
        /// 上次登录时间
        /// </summary>
        public DateTime LastLoginTime
        {
            get { return lastLoginTime; }
            set { lastLoginTime = value; }
        }

        /// <summary>
        /// 本次登录时间
        /// </summary>
        public DateTime LoginTime
        {
            get { return loginTime; }
            set { loginTime = value; }
        }

        /// <summary>
        /// 扩展信息
        /// </summary>
        public string Extension
        {
            get { return extension; }
            set { extension = value; }
        }

        #endregion

        #region 构造函数

        public User()
        {

        }

        public User(string id, string pwd, string email, string roleName, string lastLoginIp, DateTime lastLoginTime, string loginIp)
        {
            this.id = id;
            this.pwd = pwd;
            this.email = email;
            this.roleName = roleName;
            this.loginIp = loginIp;
            this.lastLoginIp = lastLoginIp;
            this.lastLoginTime = lastLoginTime;
            this.loginTime = DateTime.Now;
        }

        public User(string id, string pwd, string email, string roleName, string lastLoginIp, string lastLoginTime)
        {
            this.id = id;
            this.pwd = pwd;
            this.email = email;
            this.roleName = roleName;
            this.lastLoginIp = lastLoginIp;
            DateTime.TryParse(lastLoginTime, out this.lastLoginTime);
            this.loginTime = DateTime.Now;
        }

        #endregion

        #region 成员函数

        public override string ToString()
        {
            return String.Format(format,
                "{",
                id == null ? "null" : String.Concat("\"", id, "\""),
                pwd == null ? "null" : String.Concat("\"", pwd, "\""),
                email == null ? "null" : String.Concat("\"", email, "\""),
                roleName == null ? "null" : String.Concat("\"", roleName, "\""),
                lastLoginIp == null ? "null" : String.Concat("\"", lastLoginIp, "\""),
                lastLoginTime == DateTime.MinValue ? "null" : String.Concat("\"", lastLoginTime.ToString(), "\""),
                loginIp == null ? "null" : String.Concat("\"", loginIp, "\""),
                loginTime == DateTime.MinValue ? "null" : String.Concat("\"", loginTime.ToString(), "\""),
                "}"
                );
        }

        #endregion
    }
}
